#region imported Namespaces
using System;
using System.Data;

using DataAccess = PHSRAG.Utility.DataAccess;
using SalaryConstants = PHSRAG.Insight21.Personnel.Constants.SalaryAdjustmentsConstants;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	#region Salary Class
	/// <summary>
	/// The Salary class is the facade over the details for a single salary adjustment.
	/// </summary>
	public class Salary
	{		
		#region Instance Variables
		private DataRow salaryRow;
		#endregion
		
		#region Public Properties
		/// <summary>
		/// Get the id of the retrieved salary adjustment
		/// Return 0 if the present object is being newly added
		/// </summary>
		public int SalaryID
		{
			get
			{
				object salaryID = salaryRow[SalaryConstants.SalaryIDColumnName];
				return (salaryID==DBNull.Value)?0:(int)salaryID;
			}
		}
		/// <summary>
		/// Get/Set the employee job ID associated with the salary adjustment
		/// Return 0 if the present object is being newly added
		/// </summary>
		public int EmployeeJobMapID
		{
			get
			{
				object employeeJobMapID = salaryRow[SalaryConstants.EmployeeJobMapIDColumnName];
				return (employeeJobMapID==DBNull.Value)?0:(int)employeeJobMapID;
			}
			set
			{
				salaryRow[SalaryConstants.EmployeeJobMapIDColumnName] = value;
			}
		}
		/// <summary>
		/// Get/Set the annual rate for the salary adjustment
		/// Returns zero if there is no salary allocated
		/// </summary>
		public decimal AnnualRate
		{
			get
			{
				object annualRate = salaryRow[SalaryConstants.SalaryColumnName];
				return (annualRate == DBNull.Value)?0:(decimal)annualRate;
			}
			set
			{
				salaryRow[SalaryConstants.SalaryColumnName] = value;
			}
		}
		/// <summary>
		/// Get/Set the salary effective date
		/// Return empty string if there is no effective date
		/// </summary>
		public string EffectiveDate
		{
			get
			{
				object effectiveDate = salaryRow[SalaryConstants.EffectiveDateColumnName];
				return (effectiveDate == DBNull.Value)?
					string.Empty:((DateTime)effectiveDate).ToString("MM/dd/yyyy");
			}
			set
			{
				salaryRow[SalaryConstants.EffectiveDateColumnName] = value;
			}
		}
		/// <summary>
		/// Get/Set the user name who last edited this salary adjustment
		/// Return empty string if no edited by data is available
		/// </summary>
		public string EditedBy
		{
			get
			{
				object editedBy = salaryRow[SalaryConstants.EditedByColumnName];
				return (editedBy == DBNull.Value)?string.Empty:(string)editedBy;
			}
			set
			{
				salaryRow[SalaryConstants.EditedByColumnName] = value;
			}
		}
		/// <summary>
		/// Get/Set the date on which the salary adjustment was last edited
		/// </summary>
		public string EditDate
		{
			get
			{
				object editDate = salaryRow[SalaryConstants.EditDateColumnName];
				return (editDate==DBNull.Value)?
					string.Empty:((DateTime)editDate).ToString("MM/dd/yyyy");
			}
			set
			{
				salaryRow[SalaryConstants.EditDateColumnName] = value;
			}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Set the dataRow to the local instance
		/// </summary>
		/// <param name="dataRow">DataRow containing salary adjustment data</param>
		public Salary(DataRow dataRow)
		{
			salaryRow = dataRow;
		}
		#endregion

		#region Instance methods (Public)
		/// <summary>
		/// Add a new salary row to the database
		/// Throw an exception if any error in the add operation
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="domainUserID">Domain user id of the logged in user</param>
		public void AddSalary(string connectionString,int domainUserID)
		{
			DataTable salaryTable = salaryRow.Table;

			ValidationsHelper.ValidateEffectiveDate
				(salaryTable.DataSet,Convert.ToDateTime(EffectiveDate));

			//Validate only if there are already existing salary adjustments 
			if(salaryTable.Rows.Count > 1)
				ValidationsHelper.ValidateEffectiveDateforSalaryAdd
					(salaryTable,Convert.ToDateTime(EffectiveDate));
			try
			{
				Utility.DataAccess.ExecuteNonQuery(connectionString
					,SalaryConstants.InsertSalaryAdjustmentSPROCName
					,Utility.DataAccess.BuildSqlParameterArray
					("@domainUserID",SqlDbType.Int,domainUserID
					,"@employeeJobMapId",SqlDbType.Int,EmployeeJobMapID
					,"@annualRate",SqlDbType.Money,AnnualRate
					,"@effectiveDate",SqlDbType.DateTime,EffectiveDate
					,"@modifiedBy",SqlDbType.VarChar,EditedBy
					,"@modifiedDate",SqlDbType.DateTime,EditDate));
			}
			catch(Exception ex)
			{
				throw new Exception(@"Error in adding salary",ex);
			}
		}
		/// <summary>
		/// Update an already existing salary row to the database
		/// Throw an exception if any error in the add
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="domainUserID">Domain user id of the logged in user</param>
		public void UpdateSalary(string connectionString,int domainUserID)
		{
			DataTable salaryTable = salaryRow.Table;

			ValidationsHelper.ValidateEffectiveDate
				(salaryTable.DataSet,Convert.ToDateTime(EffectiveDate));
			
			//Validate only if there are already existing salary adjustments 
			if(salaryTable.Rows.Count > 1)
                ValidationsHelper.ValidateEffectiveDateforSalaryUpdate
					(salaryTable,Convert.ToDateTime(EffectiveDate),SalaryID);

			try
			{
				Utility.DataAccess.ExecuteNonQuery(connectionString
					,SalaryConstants.UpdateSalaryAdjustmentSPROCName
					,Utility.DataAccess.BuildSqlParameterArray
					("@domainUserID",SqlDbType.Int,domainUserID
					,"@salaryAdjustmentID",SqlDbType.Int,SalaryID
					,"@employeeJobMapID",SqlDbType.Int,EmployeeJobMapID
					,"@annualRate",SqlDbType.Money,AnnualRate
					,"@effectiveDate",SqlDbType.DateTime,EffectiveDate
					,"@modifiedBy",SqlDbType.VarChar,EditedBy
					,"@modifiedDate",SqlDbType.DateTime,EditDate));
			}
			catch(Exception ex)
			{
				throw new Exception(@"Error in updating salary",ex);
			}
		}
		#endregion
	}

	#endregion
}